package com.yizn.learn.beautiful.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yizn.learn.beautiful.config.APIException;
import com.yizn.learn.beautiful.config.NotControllerResponseAdvice;
import com.yizn.learn.beautiful.config.ResultVo;
import com.yizn.learn.beautiful.config.AppCode;
import com.yizn.learn.beautiful.service.ProductInfoService;
import com.yizn.learn.beautiful.vo.ProductInfo;
import com.yizn.learn.beautiful.vo.ProductInfoQueryVo;
import com.yizn.learn.beautiful.vo.ProductInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * project : my-learn
 * <p> 优雅的对controller层的处理，参数校验 统一响应 统一异常处理  </p>
 * <p> https://mp.weixin.qq.com/s/3KrCJOD2fhEC5-EIhZJlwA  </p>
 *
 * @author yisl
 * @date 2023-01-13【星期五】
 **/
@RestController
@RequestMapping("/product/product-info")
public class ProductInfoController {

    @Autowired
    private ProductInfoService productInfoService;

    @GetMapping("/findById")
    public ResultVo findById(Integer id){
        return new ResultVo(productInfoService.selectById(id));
    }

    @PostMapping("/page")
    public IPage findPage(Page page, ProductInfoQueryVo vo){
        return null;
    }

    @PostMapping("/find")
    public ProductInfo findByVo(@RequestBody @Validated ProductInfoVo vo) {
        ProductInfo productInfo = new ProductInfo();
        BeanUtils.copyProperties(vo, productInfo);
        return productInfoService.selectInfo(productInfo);
    }

    @NotControllerResponseAdvice
    @PostMapping("/findName")
    public String find(@RequestBody @Validated ProductInfoVo vo) {
        ProductInfo productInfo = new ProductInfo();
        BeanUtils.copyProperties(vo, productInfo);
        return "success";
    }

    @PostMapping("/findError")
    public ProductInfo findError(@RequestBody @Validated ProductInfoVo vo) {
        ProductInfo productInfo = new ProductInfo();
        BeanUtils.copyProperties(vo, productInfo);
        if (vo.getProductStatus() == 0) {
            productInfoService.selectError();
        }
        return productInfo;
    }
}
